﻿<link href="/SiteUI/Admin/pvOrgTree.css" rel="stylesheet" />
<script src="/SiteUI/res_sobiz.js"></script>

<div class="col-lg-12"> 
    <div class="row" id="pot-ortTitle">
        <h4>组织架构</h4>    

    </div>
    <div class="row">
        <div class="col-lg-12" id="pot-orgTree">

        </div>
    </div>
</div>
<script>
    pvOrganizeTree = {
        orgtree:[],
        onUserReady: function () {
            $.getJSON("/Admin/GetOrganizeTree", { company: -1 }, pvOrganizeTree.onGetOrgTreeEnd);
        },
        onGetOrgTreeEnd: function (rtn) {
            if (rtn.IsPositive) {
                //构造组织架构树
                pvOrganizeTree.orgtree = rtn.Data.orgtree;
                pvOrganizeTree.lstOrgTree.vl(pvOrganizeTree.orgtree)
                                         .update();
                pvOrganizeTree.lstOrgTree.on("itemSlcted", pvOrganizeTree.onOrgSlcted);
            }
        },
        onOrgSlcted: function (ev, data) {
            if (pvOrganizeTree.vf_onOrgSlcted) {
                pvOrganizeTree.vf_onOrgSlcted(data);
            }
        },
        lstOrgTree: null,
        GetOrgPositions: function (orgid, fnCallback) {
            $.getJSON("/Admin/GetOrgPositions", { objid: orgid }, function (rtn) {
                if (rtn.IsPositive) {
                    var org = pvOrganizeTree.orgtree.findObj("idstr",rtn.Data.orgidstr)
                    if (org) {
                        org.positions = rtn.Data["positions"];
                    }
                    if (fnCallback) {
                        fnCallback(org);
                    }
                }
            });
        },
        addChildOrg: function (norg) {
            norg.remark = norg.remark || "未添加描述";
            $.getJSON("/Admin/NewOrganize", norg, pvOrganizeTree.addChildOrgEnd);
        },
        addChildOrgEnd: function (rtn) {
            if (rtn.IsPositive) {
                success("成功", "已添加新的组织架构节点");
                pvOrganizeTree.orgtree.push(rtn.Data);
                pvOrganizeTree.lstOrgTree.vl(pvOrganizeTree.orgtree)
                                         .update()
                                         .show();
            }
            else {
                error("失败", ErrFormate(res_sobiz, rtn.ErrorSrc, rtn.ErrorCode, rtn.Error));
            }
        },
        updateOrg: function (org, callback) {
            var o = pvOrganizeTree.lstOrgTree.vl().findObj("idstr", org.objid);
            o.name = org.name;
            o.remark = org.remark;
            o.parentidstr = org.parentid;

            pvOrganizeTree.lstOrgTree.vl().findObj("idstr", org.objid, o);
            pvOrganizeTree.lstOrgTree.update().show();
            $.getJSON("/Admin/UpdateOrgInfo", org, callback);
        },
        vf_onOrgSlcted:function(ev){}
    }
    $(function () {
        pvOrganizeTree.lstOrgTree = new LiberUI.Lister("#pot-orgTree")
                                                    .option("TreeMode", true)
                                                    .option("ContentRender", function ($item,o) {
                                                        $("<div class='pot-orgItem'>").vl(o.name).appendTo($item);
                                                    })
                                                    .Loading("正在加载...");
        $("body").on("UserReady", pvOrganizeTree.onUserReady);
    })
</script>

